package com.haisen.controller;


import com.alibaba.fastjson.JSON;
import com.haisen.core.annotation.SystemLog;
import com.haisen.core.utils.Result;
import com.haisen.core.utils.ResultCodeEnum;
import com.haisen.dataobject.dto.ItemCatDto;
import com.haisen.dataobject.dto.TreeDto;
import com.haisen.dataobject.entity.ItemCat;
import com.haisen.exception.BizException;
import com.haisen.service.ItemCatService;
import com.haisen.service.ItemParamGroupService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

@Controller
@RequestMapping("/itemcat")
@Slf4j
public class ItemCatController {

    @Autowired
    private ItemCatService itemCatService;
    @Autowired
    private ItemParamGroupService itemParamGroupService;

    @GetMapping
    @RequiresPermissions("itemcat:view")
    public String page(Model model){
      /*  Weekend example = Weekend.of(ItemCat.class);
        example.setOrderByClause("sort_order");
        List<ItemCatDto> itemCatDtoList = itemCatService.findItemCatDto(example);*/
        /**
         * 有个注解sql后，上面就太复杂了。
         */
        List<ItemCatDto> itemCatDtoList = itemCatService.selectCatAndGroups();
        model.addAttribute("itemCatDtoList",itemCatDtoList);
        model.addAttribute("groups",itemParamGroupService.findAll());
        return "product/itemcat";
    }


    @ResponseBody
    @RequestMapping("/tree")
    public List<TreeDto> findOrgTree(Long pId) {
        return itemCatService.findItemCatTree(pId);
    }


    @ResponseBody
    @RequiresPermissions("itemcat:view")
    @RequestMapping(value = "{id}/load", method = RequestMethod.POST)
    public Result load(@PathVariable Long id) {
        ItemCat itemCat = itemCatService.findOneById(id);
        return Result.success(itemCat);
    }

    @ResponseBody
    @RequiresPermissions("itemcat:create")
    @SystemLog("产品种类管理创建类别")
    @PostMapping("/create")
    public Result create(@Valid ItemCatDto itemCatDto) {
        ItemCat itemCat = new ItemCat();
        try {
            BeanUtils.copyProperties(itemCat,itemCatDto);
            //多重映射，这里重新拷贝属性
        } catch (IllegalAccessException e) {
            throw new BizException(ResultCodeEnum.FAILED_ITEM_COPY_PROPERIIES);
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        itemCatService.create(itemCat);
        return Result.success();
    }

    @ResponseBody
    @RequiresPermissions("itemcat:update")
    @SystemLog("产品种类管理更新类别")
    @PostMapping("/update")
    public Result update(@Valid ItemCatDto itemCatDto) {
        ItemCat itemCat = new ItemCat();
        try {
            BeanUtils.copyProperties(itemCat,itemCatDto);
        } catch (IllegalAccessException e) {
        throw new BizException(ResultCodeEnum.FAILED_ITEM_COPY_PROPERIIES);
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        log.info("修改对象值={}", JSON.toJSONString(itemCat));
        itemCatService.update(itemCat);
        return Result.success();
    }

    @ResponseBody
    @RequiresPermissions("itemcat:delete")
    @SystemLog("产品种类管理删除类别")
    @PostMapping("/delete")
    public Result delete(Long id) {
        itemCatService.delete(id);
        return Result.success();
    }

    /**
     * 根据catId查找paramgroup
     */
    @ResponseBody
    @RequestMapping("/groups")
    public Result findGroups(@RequestParam("id") Long id){
        return Result.success(itemCatService.selectGroups(id));
    }

    @ResponseBody
    @RequestMapping("/paramkeys")
    public Result findParmkeys(@RequestParam("id") Long id){
        return Result.success(itemCatService.selectParamKeys(id));
    }
}
